// .........................................................................
// Title: variation_histograms.do
//
// Constructs histograms showing the distribution of the relevant sample
// variation, both unconditionally and within the fixed effects
// .........................................................................

* Variation histograms sample: drawdowns
use "$tmp/variation_histograms_sample", clear
bys full_fe_category: gen N_within = _N
bys full_fe_category: egen mean_hedged_fe_cat = mean(drawdown_hedged)
gen drawdown_hedged_fe_cat = drawdown_hedged - mean_hedged_fe_cat
replace drawdown_hedged_fe_cat = . if N_within == 1
cap drop N_within
save "$tmp/figdata_hist_d", replace

* Variation histograms sample: ownership
use "$tmp/variation_histograms_sample", clear
replace share_ins_issuance = . if share_ins_issuance > 1
bys full_fe_category: gen N_within = _N
bys full_fe_category: egen mean_hedged_fe_cat = mean(share_ins_issuance)
gen share_ins_issuance_fe_cat = share_ins_issuance - mean_hedged_fe_cat
replace share_ins_issuance_fe_cat = . if N_within == 1
cap drop N_within
save "$tmp/figdata_hist_o", replace

* Unconditional histograms
use "$tmp/figdata_hist_d", clear
hist drawdown_hedged, graphregion(color(white)) xtitle("Drawdown") ytitle("Sample Density" " ") ///
    lcolor(blue) fcolor(blue%10) title("(A) Unconditional Distribution" " ", size(medsmall) color(black)) ///
    saving("$tmp/hist_d_unconditional.gph", replace) frac

use "$tmp/figdata_hist_o", clear
hist share_ins_issuance, graphregion(color(white)) xtitle("Insurer Share") ytitle("Sample Density" " ") ///
    lcolor(blue) fcolor(blue%10) title("", size(medsmall) color(black)) ///
    saving("$tmp/hist_o_unconditional.gph", replace) frac

* Residual histograms
use "$tmp/figdata_hist_d", clear
hist drawdown_hedged_fe_cat if inrange(drawdown_hedged_fe_cat, -.2, .2), ///
    graphregion(color(white)) xtitle("Residual Drawdown") ytitle("" " ") ///
    lcolor(red) fcolor(red%10) title("(B) Residualized vs. Fixed Effects" " ", size(medsmall) color(black)) ///
    saving("$tmp/hist_d_residual.gph", replace) frac xlab(-.2(.1).2) ylab(, gmin gmax)

use "$tmp/figdata_hist_o", clear
hist share_ins_issuance_fe_cat if inrange(share_ins_issuance_fe_cat, -.4, .4), ///
    graphregion(color(white)) xtitle("Residual Insurer Share") ytitle("" " ") ///
    lcolor(red) fcolor(red%10) title("", size(medsmall) color(black)) ///
    saving("$tmp/hist_o_residual.gph", replace) frac xlab(-.4(.2).4)

* Merged histograms (Figure 6)
gr combine "$tmp/hist_d_unconditional.gph" ///
        "$tmp/hist_d_residual.gph" ///
        "$tmp/hist_o_unconditional.gph" ///
        "$tmp/hist_o_residual.gph", ///
    graphregion(color(white)) xsize(7) cols(2)

graph export "$graphs/variation_histograms.pdf", as(pdf) replace
